import { GETFILTERPRODUCT, GETFILTERPRODUCT_SUCCESS, GETPRODUCTBYID, GETPRODUCTBYID_SUCCESS, GET_PRODUCT, GET_PRODUCT_BY_SEARCHVALUE, GET_PRODUCT_BY_SEARCHVALUE_SUCCESS, GET_PRODUCT_SUCCESS, ProductUnionType } from "../actions/product.action";
import { Product, ProductFilterData } from "../models/product";

export interface ProductState {
    createdAt: {
        loaded: boolean
        success: boolean
        result: Product[]
    },
    sold: {
        loaded: boolean
        success: boolean
        result: Product[]
    },
    search: {
        loaded: boolean
        success: boolean
        result: Product[]
    },
    filterProduct: {
        loaded: boolean
        success: boolean
        result: ProductFilterData
    },
    productByID: {
        loaded: boolean
        success: boolean
        result: Product
    }
}
const initialState: ProductState = {
    createdAt: {
        loaded: false,
        success: false,
        result: []
    },
    sold: {
        loaded: false,
        success: false,
        result: []
    },
    search: {
        loaded: false,
        success: false,
        result: []
    },
    filterProduct: {
        loaded: false,
        success: false,
        result: {
            data: [],
            size: 0
        }
    },
    productByID: {
        loaded: false,
        success: false,
        result: {
            "sold": 0,
            "_id": '-1',
            "name": '',
            "description": '',
            "price": 0,
            "category": {
                name: '',
                _id: ''
            },
            "quantity": 0,
            "shipping": false,
            "createdAt": '',
            "updatedAt": '',
        }
    }

}
export default function productReducer(state = initialState, action: ProductUnionType) {
    switch (action.type) {
        case GET_PRODUCT:
            return {
                ...state,
                [action.sortBy]: {
                    ...state[action.sortBy === 'createdAt' ? 'createdAt' : 'sold'],
                    loaded: false,
                    success: false,
                }
            }
        case GET_PRODUCT_SUCCESS:
            return {
                ...state,
                [action.sortBy]: {
                    loaded: true,
                    success: true,
                    result: action.payload
                }
            }
        case GET_PRODUCT_BY_SEARCHVALUE:
            return {
                ...state,
                search: {
                    loaded: false,
                    success: false,
                    result: []
                }
            }
        case GET_PRODUCT_BY_SEARCHVALUE_SUCCESS:
            return {
                ...state,
                search: {
                    loaded: true,
                    success: true,
                    result: action.payload
                }
            }
        case GETFILTERPRODUCT:
            return {
                ...state,
                filterProduct: {
                    loaded: false,
                    success: false,
                    result: {
                        data: state.filterProduct.result.data,
                        size: 0
                    }
                }
            }
        case GETFILTERPRODUCT_SUCCESS:
            return {
                ...state,
                filterProduct: {
                    loaded: false,
                    success: false,
                    result: {
                        size: action.payload.size,
                        data: action.skip === 0 ? action.payload.data : [...state.filterProduct.result.data, ...action.payload.data]
                    }
                }
            }
        case GETPRODUCTBYID:
            return {
                ...state,
                productByID: {
                    ...state.productByID,
                    loaded: false,
                    success: false,
                }
            }
        case GETPRODUCTBYID_SUCCESS:
            return {
                ...state,
                productByID: {
                    loaded: true,
                    success: true,
                    result: action.result
                }
            }
        default:
            return state;
    }
}